package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarSeriesDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.param.DeleteCarSeriesParam;
import com.woniu108.car.form.InsertCarSeriesForm;
import com.woniu108.car.form.UpdateCarSeriesNameForm;
import com.woniu108.car.model.CarSeries;
import com.woniu108.car.param.BanCarSeriesParam;
import com.woniu108.car.param.InsertCarSeriesParam;
import com.woniu108.car.param.UnlockCarSeriesParam;
import com.woniu108.car.param.UpdateCarSeriesNameParam;
import com.woniu108.car.service.CarSeriesService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carSeries")
@Api(tags = "汽车系列管理")
public class CarSeriesController {

    @Resource
    private CarSeriesService carSeriesService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询某品牌汽车所有未删除系列
     * @param brandId 品牌id
     * @return CarSeries集合
     */
    @GetMapping("/findAllSeriesByBrandId.api")
    @ApiOperation("查询某品牌汽车所有未删除系列")
    public Result<List<CarSeriesDto>> findAllSeriesByBrandId(Integer brandId) {
        List<CarSeries> list = carSeriesService.findAllSeriesByBrandId(brandId);
        List<CarSeriesDto> dtos = BeanUtil.copyToList(list, CarSeriesDto.class);

        return Result.newListInstance(CarSeriesDto.class).setData(dtos).setMsg("查询某品牌汽车所有未删除系列成功");
    }
    /**
     * 查询某品牌汽车所有未删除系列
     * @param brandId 品牌id
     * @return CarSeries集合
     */
    @GetMapping("/findAllNormalSeriesByBrandId")
    @ApiOperation("查询某品牌汽车所有正常系列成功")
    @ApiImplicitParam(name = "brandId",value = "品牌id")
    public Result<List<CarSeriesDto>> findAllNormalSeriesByBrandId(Integer brandId) {
        List<CarSeries> list = carSeriesService.findAllNormalSeriesByBrandId(brandId);
        List<CarSeriesDto> dtos = BeanUtil.copyToList(list, CarSeriesDto.class);

        return Result.newListInstance(CarSeriesDto.class).setData(dtos).setMsg("查询某品牌汽车所有正常系列成功");
    }
    /**
     * 新增品牌系列
     * @param form 品牌id,系列名称
     * @return boolean值
     */
    @PostMapping("/insertCarSeries.api")
    @ApiOperation("新增品牌系列")@AdminHistoryLog
    public Result<Boolean> insertCarSeries(@RequestBody InsertCarSeriesForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarSeriesParam param = BeanUtil.toBean(form, InsertCarSeriesParam.class);
        param.setAdminId(adminId);
        Boolean bool = carSeriesService.insertCarSeries(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增汽车品牌系列成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增汽车品牌系列失败").setData(false).setCode(500);
    }
    /**
     * 删除汽车某品牌系列
     * @param seriesIds 系列id
     * @return boolean值
     */
    @PostMapping("/deleteCarSeries.api")
    @ApiOperation("删除汽车某品牌系列")@AdminHistoryLog
    @ApiImplicitParam(name = "seriesIds",value = "系列id集合")
    public Result<Boolean> deleteCarSeries(@RequestBody List<Integer> seriesIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteCarSeriesParam param = new DeleteCarSeriesParam(seriesIds, adminId);
        Boolean bool = carSeriesService.deleteCarSeries(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除汽车某品牌系列成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除汽车某品牌系列失败").setData(false).setCode(500);
    }
    /**
     * 禁用汽车某品牌系列
     * @param seriesIds 系列id
     * @return boolean值
     */
    @PostMapping("/banCarSeries.api")
    @ApiOperation("禁用某品牌汽车某系列")@AdminHistoryLog
    @ApiImplicitParam(name = "seriesIds",value = "系列id集合")
    public Result<Boolean> banCarSeries(@RequestBody List<Integer> seriesIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanCarSeriesParam param = new BanCarSeriesParam(seriesIds, adminId);
        Boolean bool = carSeriesService.banCarSeries(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用某品牌汽车某系列成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用某品牌汽车某系列失败").setData(false).setCode(500);
    }
    /**
     * 解锁汽车某品牌系列
     * @param seriesIds 系列id
     * @return boolean值
     */
    @PostMapping("/unlockCarSeries.api")
    @ApiOperation("解锁汽车系列")@AdminHistoryLog
    @ApiImplicitParam(name = "seriesIds",value = "系列id集合")
    public Result<Boolean> unlockCarSeries(@RequestBody List<Integer> seriesIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockCarSeriesParam param = new UnlockCarSeriesParam(seriesIds, adminId);
        Boolean bool = carSeriesService.unlockCarSeries(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁汽车系列成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁汽车系列失败").setData(false).setCode(500);
    }
    /**
     *
     * 修改汽车某品牌系列名称
     * @param form 系列id,新名称
     * @return boolean值
     */
    @PostMapping("/updateCarSeriesName.api")
    @ApiOperation("修改系列名称")@AdminHistoryLog
    public Result<Boolean> updateCarSeriesName(@RequestBody UpdateCarSeriesNameForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateCarSeriesNameParam param = BeanUtil.toBean(form, UpdateCarSeriesNameParam.class);
        param.setAdminId(adminId);
        Boolean bool = carSeriesService.updateCarSeriesName(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改系列名称成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改系列名称失败").setData(false).setCode(500);

    }
}

